<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">字典类型</div>
        <a-button type="primary" @click="add">新增</a-button>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postDictPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a-space :size="10" class="space-btn--box">
                <a class="btn" @click="edit(record)">修改</a>
                <a class="btn" @click="addSub(record)">添加子类</a>
                <a class="btn red-btn" @click="del(record.id)">删除</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
    <!-- 修改 -->
    <ModalDictionaryEdit
      ref="modalDictionaryEditRef"
      :data="dataDetail"
      @ok="tableRef.refreshTableData()"
    ></ModalDictionaryEdit>
  </PageLayout>
</template>

<script setup>
// 组件
import ModalDictionaryEdit from "./components/modal-dictionary-edit.vue";
import { postDictPage, postDictDelete } from "@/api/system";
import { dictStatusOptions } from "@/views/constant";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { message, Modal } from "ant-design-vue";
import { getArrayObj } from "@/utils/common";
const router = useRouter();
const queryOptions = [
  {
    name: "字典名称",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "dictName",
  },
  {
    name: "状态",
    type: "select",
    value: undefined,
    placeholder: "请选择",
    filed: "dictStatus",
    list: dictStatusOptions,
    attrs: { allowClear: true },
  },
  //   {
  //     name: "创建时间",
  //     type: "range-picker",
  //     value: [],
  //     placeholder: ["开始日期", "结束日期"],
  //     filed: "gmtCreate",
  //     attrs: { format: "YYYY-MM-DD HH:mm:ss", showTime: true },
  //   },
];

const columns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    width: 60,
    customRender: (record) => `${record.index + 1}`,
  },
  { title: "字典名称", dataIndex: "dictName", key: "dictName", ellipsis: true },
  { title: "字典类型", dataIndex: "dictType", key: "dictType", ellipsis: true },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    ellipsis: true,
    customRender: ({ record }) =>
      getArrayObj(dictStatusOptions, record.status)?.label,
  },
  { title: "备注", dataIndex: "remark", key: "remark", ellipsis: true },
  //   {
  //     title: "创建人",
  //     dataIndex: "createdUserName",
  //     key: "createdUserName",
  //     ellipsis: true,
  //   },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
    ellipsis: true,
  },
  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];
const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const modalDictionaryEditRef = ref(null);
const dataDetail = ref({});
// 新增字典
const add = () => {
  dataDetail.value = {};
  setTimeout(() => {
    modalDictionaryEditRef.value.visible = true;
  }, 0);
};
// 修改字典
const edit = (record) => {
  dataDetail.value = record;
  setTimeout(() => {
    modalDictionaryEditRef.value.visible = true;
  }, 0);
};
// 删除字典
const del = (id) => {
  Modal.confirm({
    icon: h(ExclamationCircleOutlined),
    title: () => "删除确认",
    content: () => "删除后将无法恢复，确认删除？",
    onOk() {
      postDictDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.refreshTableData();
      });
    },
  });
};

// 添加子类
const addSub = (record) => {
  router.push({
    path: "/sys/dict/dataList",
    query: {
      id: record.id,
      dictName: record.dictName,
      dictType: record.dictType,
    },
  });
};
</script>

<style lang="less" scoped></style>
